home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / unittest / runner.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  7KB  |  215 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Running tests'''
  5. import sys
  6. import time
  7. from  import result
  8. from signals import registerResult
  9. __unittest = True
  10.  
  11. class _WritelnDecorator(object):
  12.     """Used to decorate file-like objects with a handy 'writeln' method"""
  13.     
  14.     def __init__(self, stream):
  15.         self.stream = stream
  16.  
  17.     
  18.     def __getattr__(self, attr):
  19.         if attr in ('stream', '__getstate__'):
  20.             raise AttributeError(attr)
  21.         return getattr(self.stream, attr)
  22.  
  23.     
  24.     def writeln(self, arg = None):
  25.         if arg:
  26.             self.write(arg)
  27.         self.write('\n')
  28.  
  29.  
  30.  
  31. class TextTestResult(result.TestResult):
  32.     '''A test result class that can print formatted text results to a stream.
  33.  
  34.     Used by TextTestRunner.
  35.     '''
  36.     separator1 = '=' * 70
  37.     separator2 = '-' * 70
  38.     
  39.     def __init__(self, stream, descriptions, verbosity):
  40.         super(TextTestResult, self).__init__(stream, descriptions, verbosity)
  41.         self.stream = stream
  42.         self.showAll = verbosity > 1
  43.         self.dots = verbosity == 1
  44.         self.descriptions = descriptions
  45.  
  46.     
  47.     def getDescription(self, test):
  48.         doc_first_line = test.shortDescription()
  49.         if self.descriptions and doc_first_line:
  50.             return '\n'.join((str(test), doc_first_line))
  51.         return None(test)
  52.  
  53.     
  54.     def startTest(self, test):
  55.         super(TextTestResult, self).startTest(test)
  56.         if self.showAll:
  57.             self.stream.write(self.getDescription(test))
  58.             self.stream.write(' ... ')
  59.             self.stream.flush()
  60.  
  61.     
  62.     def addSuccess(self, test):
  63.         super(TextTestResult, self).addSuccess(test)
  64.         if self.showAll:
  65.             self.stream.writeln('ok')
  66.         elif self.dots:
  67.             self.stream.write('.')
  68.             self.stream.flush()
  69.  
  70.     
  71.     def addError(self, test, err):
  72.         super(TextTestResult, self).addError(test, err)
  73.         if self.showAll:
  74.             self.stream.writeln('ERROR')
  75.         elif self.dots:
  76.             self.stream.write('E')
  77.             self.stream.flush()
  78.  
  79.     
  80.     def addFailure(self, test, err):
  81.         super(TextTestResult, self).addFailure(test, err)
  82.         if self.showAll:
  83.             self.stream.writeln('FAIL')
  84.         elif self.dots:
  85.             self.stream.write('F')
  86.             self.stream.flush()
  87.  
  88.     
  89.     def addSkip(self, test, reason):
  90.         super(TextTestResult, self).addSkip(test, reason)
  91.         if self.showAll:
  92.             self.stream.writeln('skipped {0!r}'.format(reason))
  93.         elif self.dots:
  94.             self.stream.write('s')
  95.             self.stream.flush()
  96.  
  97.     
  98.     def addExpectedFailure(self, test, err):
  99.         super(TextTestResult, self).addExpectedFailure(test, err)
  100.         if self.showAll:
  101.             self.stream.writeln('expected failure')
  102.         elif self.dots:
  103.             self.stream.write('x')
  104.             self.stream.flush()
  105.  
  106.     
  107.     def addUnexpectedSuccess(self, test):
  108.         super(TextTestResult, self).addUnexpectedSuccess(test)
  109.         if self.showAll:
  110.             self.stream.writeln('unexpected success')
  111.         elif self.dots:
  112.             self.stream.write('u')
  113.             self.stream.flush()
  114.  
  115.     
  116.     def printErrors(self):
  117.         if self.dots or self.showAll:
  118.             self.stream.writeln()
  119.         self.printErrorList('ERROR', self.errors)
  120.         self.printErrorList('FAIL', self.failures)
  121.  
  122.     
  123.     def printErrorList(self, flavour, errors):
  124.         for test, err in errors:
  125.             self.stream.writeln(self.separator1)
  126.             self.stream.writeln('%s: %s' % (flavour, self.getDescription(test)))
  127.             self.stream.writeln(self.separator2)
  128.             self.stream.writeln('%s' % err)
  129.         
  130.  
  131.  
  132.  
  133. class TextTestRunner(object):
  134.     '''A test runner class that displays results in textual form.
  135.  
  136.     It prints out the names of tests as they are run, errors as they
  137.     occur, and a summary of the results at the end of the test run.
  138.     '''
  139.     resultclass = TextTestResult
  140.     
  141.     def __init__(self, stream = sys.stderr, descriptions = True, verbosity = 1, failfast = False, buffer = False, resultclass = None):
  142.         self.stream = _WritelnDecorator(stream)
  143.         self.descriptions = descriptions
  144.         self.verbosity = verbosity
  145.         self.failfast = failfast
  146.         self.buffer = buffer
  147.         if resultclass is not None:
  148.             self.resultclass = resultclass
  149.  
  150.     
  151.     def _makeResult(self):
  152.         return self.resultclass(self.stream, self.descriptions, self.verbosity)
  153.  
  154.     
  155.     def run(self, test):
  156.         '''Run the given test case or test suite.'''
  157.         result = self._makeResult()
  158.         registerResult(result)
  159.         result.failfast = self.failfast
  160.         result.buffer = self.buffer
  161.         startTime = time.time()
  162.         startTestRun = getattr(result, 'startTestRun', None)
  163.         if startTestRun is not None:
  164.             startTestRun()
  165.         
  166.         try:
  167.             test(result)
  168.         finally:
  169.             stopTestRun = getattr(result, 'stopTestRun', None)
  170.             if stopTestRun is not None:
  171.                 stopTestRun()
  172.  
  173.         stopTime = time.time()
  174.         timeTaken = stopTime - startTime
  175.         result.printErrors()
  176.         if hasattr(result, 'separator2'):
  177.             self.stream.writeln(result.separator2)
  178.         run = result.testsRun
  179.         if not run != 1 or 's':
  180.             pass
  181.         self.stream.writeln('Ran %d test%s in %.3fs' % (run, '', timeTaken))
  182.         self.stream.writeln()
  183.         expectedFails = unexpectedSuccesses = skipped = 0
  184.         
  185.         try:
  186.             results = map(len, (result.expectedFailures, result.unexpectedSuccesses, result.skipped))
  187.         except AttributeError:
  188.             pass
  189.  
  190.         (expectedFails, unexpectedSuccesses, skipped) = results
  191.         infos = []
  192.         if not result.wasSuccessful():
  193.             self.stream.write('FAILED')
  194.             (failed, errored) = map(len, (result.failures, result.errors))
  195.             if failed:
  196.                 infos.append('failures=%d' % failed)
  197.             if errored:
  198.                 infos.append('errors=%d' % errored)
  199.             
  200.         else:
  201.             self.stream.write('OK')
  202.         if skipped:
  203.             infos.append('skipped=%d' % skipped)
  204.         if expectedFails:
  205.             infos.append('expected failures=%d' % expectedFails)
  206.         if unexpectedSuccesses:
  207.             infos.append('unexpected successes=%d' % unexpectedSuccesses)
  208.         if infos:
  209.             self.stream.writeln(' (%s)' % (', '.join(infos),))
  210.         else:
  211.             self.stream.write('\n')
  212.         return result
  213.  
  214.  
  215.